﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Lswweb.Model;
using Lswweb.IDAL;

namespace Lswweb.SqlServerDAL
{
    public class UpFilesDAL : IUpFiles 
    {
        private Builder<UpFileInfo> fileDAL = new Builder<UpFileInfo>("UpFiles", "Id");
        private string orderBy = "Id asc";

        public UpFilesDAL()
        {
            fileDAL.GetParameters += new ParametersBuilder<UpFileInfo>(GetParameters);
            fileDAL.CreateModel += new ReaderBind<UpFileInfo>(ReaderBind);
        }

        public UpFileInfo GetFile(int id)
        {
            return this.fileDAL.GetModel(id);
        }

        public IList<UpFileInfo> GetFiles(string searchKey, int itemId ,string ver ,StatusType? status,int pageIndex, int pageSize, out int totalRecord)
        {
            StringBuilder where = new StringBuilder();
            IList<IDataParameter> parameters = new List<IDataParameter>();

            parameters.Add(DataAccess.CreateParameter("@Ver",ver));
            where.Append("Ver=@Ver");

            //SearchKey类别
            if (!string.IsNullOrEmpty(searchKey))
            {
                parameters.Add(DataAccess.CreateParameter("@SearchKey", searchKey));

                where.Append(" and FileName like '%' + @SearchKey + '%'");
            }

            if (itemId > 0)
            {
                parameters.Add(DataAccess.CreateParameter("@CategoryId", itemId));
                where.Append(" and exists(select Id from UpFileCategories where [Path] + '/' like (select [Path] + '/' from UpFileCategories as UpFileCategories_1 where (Id = @CategoryId)) + '%' AND (UpFiles.ItemId = Id))");
            }

            if (status!=null && Enum.IsDefined(typeof(StatusType), status))
            {
                parameters.Add(DataAccess.CreateParameter("@Status", (int)status));
                where.Append(" and Status=@Status");
            }

            return fileDAL.GetModels(string.Empty, where.ToString(), orderBy, pageIndex, pageSize, out totalRecord, parameters.ToArray());
        }

        public int Insert(UpFileInfo info)
        {
            return fileDAL.Insert(
                info, 
                "ItemId", 
                "FileName", 
                "FileSize", 
                "FileUrl", 
                "LinkUrl", 
                "DateCreated", 
                "Ver",
                "Status");
        }

        public bool Update(UpFileInfo info)
        {
            if (info.Id <= 0)
                return false;

            return fileDAL.Update(
                info, 
                "Id=@Id", 
                "ItemId", 
                "FileName", 
                "FileSize", 
                "FileUrl", 
                "LinkUrl",
                "Status");
        }

        public bool Delete(int id)
        {
            return this.fileDAL.Delete(id);
        }

        public object GetFieldValue(int id, string fieldName)
        {
            if (id <= 0)
                return null;

            return fileDAL.GetFieldValue(fieldName, id);
        }

        #region Protected Methods

        protected IDataParameter[] GetParameters(UpFileInfo info)
        {
            return new IDataParameter[]{
                DataAccess.CreateParameter("@Id", info.Id),
                DataAccess.CreateParameter("@ItemId", (int)info.ItemId),
                DataAccess.CreateParameter("@FileName", info.FileName),
                DataAccess.CreateParameter("@FileSize", info.FileSize),
                DataAccess.CreateParameter("@FileUrl", info.FileUrl),
                DataAccess.CreateParameter("@DateCreated", info.DateCreated),
                DataAccess.CreateParameter("@Ver", info.Ver),
                DataAccess.CreateParameter("@LinkUrl", info.LinkUrl),
                DataAccess.CreateParameter("@Status",(int)info.Status)
            };
        }

        protected UpFileInfo ReaderBind(Hashtable dataReader)
        {
            UpFileInfo model = new UpFileInfo();

            if (dataReader.ContainsKey("Id"))
                model.Id = Convert.ToInt32(dataReader["Id"]);

            if (dataReader.ContainsKey("ItemId"))
                model.ItemId = Convert.ToInt32(dataReader["ItemId"]);

            if (dataReader.ContainsKey("FileSize"))
                model.FileSize = Convert.ToInt32(dataReader["FileSize"]);

            if (dataReader.ContainsKey("DateCreated"))
                model.DateCreated = (DateTime)dataReader["DateCreated"];

            if (dataReader.ContainsKey("FileName"))
                model.FileName = dataReader["FileName"].ToString();

            if (dataReader.ContainsKey("FileUrl"))
                model.FileUrl = dataReader["FileUrl"].ToString();

            if (dataReader.ContainsKey("Ver"))
                model.Ver = dataReader["Ver"].ToString();

            if (dataReader.ContainsKey("LinkUrl"))
                model.LinkUrl = dataReader["LinkUrl"].ToString();

            if (dataReader.ContainsKey("Status"))
                model.Status = (StatusType)Convert.ToInt32(dataReader["Status"]);

            return model;
        }

        #endregion

        #region ProductImages

        public IList<UpFileInfo> GetFiles(int productId)
        {
            if(productId<=0)
                return  new List<UpFileInfo>();

            string sql = "select UpFiles.* from UpFiles,ProImgShow where UpFiles.Id=ProImgShow.FileId and ProImgShow.ProId=@ProductId";
            return this.fileDAL.GetModels(sql, DataAccess.CreateParameter("@ProductId", productId));
        }

        #endregion
    }
}
